<template>
	<view>
		<u-navbar title="闹铃" :autoBack="true" rightText="添加" @rightClick="rightClick">
		</u-navbar>
		<view class="u-border-top">
			<u-swipe-action :autoClose="true">
				<u-swipe-action-item :options="options" @click="del(index)" v-for="(item,index) in list" :key="index">
					<view class="swipe-action u-border-bottom flex align-center justify-between px-3">
						<view class="swipe-action__content">
							<view class="font36 font-weight-bold mb-1  line-h">{{item.time}}
							</view>
							<view class="font24 line-h color666">
								<text v-for="(items,index1) in item.week" :key="index1">{{items}}{{(index1==(item.week.length -1)) ?'': '、'}}</text>
							</view>
						</view>
						<!-- <view class="">
							<u-switch v-model="item.enabled" @change="change(item)" size="22"></u-switch>
						</view> -->
					</view>
				</u-swipe-action-item>
			</u-swipe-action>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}],
				value: false,
				time: "",
				list: []
			};
		},
		onShow() {
			this.getData()
		},
		methods: {
			rightClick() {
				uni.navigateTo({
					url: 'addalarm/addalarm'
				})
			},
			change() {},
			del(index) {
				let _this = this;
				uni.showModal({
					title: '提示',
					content: "是否删除该闹钟？",
					success: function(res) {
						if (res.confirm) {
							_this.list.splice(index, 1)
							_this.list.map((item)=>{
								return item.week=item.week[0]
							})
							let opts = {
								url: 'task/sendalarm?imei=' + uni.getStorageSync('imei') +
									'&appType=0&editable=true&item=' +
									{alarmClock:_this.list}
							};
							let param = {
								imei: uni.getStorageSync('imei'),
								appType: 0,
								editable: true,
								item:{alarmClock:_this.list}
							};
							_this.$http.httpDeveiceRequest(opts, param).then(
								res => {
									if (res.data.ReturnMsg == "success") {
										uni.showToast({
											title: "删除成功",
											icon: "none"
										})
										_this.getData()
									}else{
										uni.showToast({
											title: "删除失败",
											icon: "none"
										})
									}
								},
								error => {
									uni.showToast({
										title: "删除失败",
										icon: "none"
									})
									console.log(error);
								}
							);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},
			getData() {
				let _this = this;

				let opts = {
					url: 'task/readalarm?imei=' + uni.getStorageSync('imei'),
				};
				let param = {
					imei: uni.getStorageSync('imei')
				};
				_this.$http.httpDeveiceRequest(opts, param).then(
					res => {
						console.log(res.data);
						if (res.data.ReturnMsg == "success") {
							let arr = [];
							let datas = res.data.ReturnData;
							datas.forEach((item, index) => {
								console.log(item, index)
								let text = "";
								if (index == 0) {
									let param = {
										time: item.time,
										week: []
									}
									if (item.weekdays == 0) {
										text = "星期日"
									} else if (item.weekdays == 1) {
										text = "星期一"
									} else if (item.weekdays == 2) {
										text = "星期二"
									} else if (item.weekdays == 3) {
										text = "星期三"
									} else if (item.weekdays == 4) {
										text = "星期四"
									} else if (item.weekdays == 5) {
										text = "星期五"
									} else if (item.weekdays == 6) {
										text = "星期六"
									}
									param.week.push(text);
									arr.push(param)
								} else {
									if (item.time == datas[index - 1].time) {
										if (item.weekdays == 0) {
											text = "星期日"
										} else if (item.weekdays == 1) {
											text = "星期一"
										} else if (item.weekdays == 2) {
											text = "星期二"
										} else if (item.weekdays == 3) {
											text = "星期三"
										} else if (item.weekdays == 4) {
											text = "星期四"
										} else if (item.weekdays == 5) {
											text = "星期五"
										} else if (item.weekdays == 6) {
											text = "星期六"
										}
										arr.forEach((items) => {
											if (items.time == item.time) {
												items.week.push(text)
											}
										})
									} else {
										let param = {
											time: item.time,
											week: []
										}
										if (item.weekdays == 0) {
											text = "星期日"
										} else if (item.weekdays == 1) {
											text = "星期一"
										} else if (item.weekdays == 2) {
											text = "星期二"
										} else if (item.weekdays == 3) {
											text = "星期三"
										} else if (item.weekdays == 4) {
											text = "星期四"
										} else if (item.weekdays == 5) {
											text = "星期五"
										} else if (item.weekdays == 6) {
											text = "星期六"
										}
										param.week.push(text);
										arr.push(param)
									}
								}
							})
							_this.list = arr;
						} else {
							_this.list = [];
						}
					},
					error => {
						console.log(error);
					}
				);
			},

		}
	}
</script>

<style lang="scss">
	.swipe-action {
		height: 120rpx;
	}
</style>
